package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.Reception;
import com.amsp.oa.oademo.mapper.ReceptionMapper;
import com.amsp.oa.oademo.service.ReceptionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Random;
import java.text.SimpleDateFormat;

@Service
public class ReceptionServiceImpl extends ServiceImpl<ReceptionMapper, Reception> implements ReceptionService {

    @Override
    public IPage<Reception> getReceptionList(Page<Reception> page, String keyword, String status) {
        LambdaQueryWrapper<Reception> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(keyword)) {
            wrapper.like(Reception::getApplicationNo, keyword)
                  .or()
                  .like(Reception::getGuestName, keyword)
                  .or()
                  .like(Reception::getGuestCompany, keyword);
        }
        
        if (StringUtils.hasText(status)) {
            wrapper.eq(Reception::getStatus, status);
        }
        
        wrapper.orderByDesc(Reception::getCreateTime);
        
        return this.page(page, wrapper);
    }

    @Override
    public Reception createReception(Reception reception) {
        reception.setCreateTime(new Date());
        reception.setUpdateTime(new Date());
        reception.setStatus("pending");
        this.save(reception);
        return reception;
    }

    @Override
    public Reception updateReception(Reception reception) {
        reception.setUpdateTime(new Date());
        this.updateById(reception);
        return reception;
    }

    @Override
    public void deleteReception(Long id) {
        this.removeById(id);
    }

    @Override
    public Reception getReceptionByNo(String applicationNo) {
        LambdaQueryWrapper<Reception> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Reception::getApplicationNo, applicationNo);
        return this.getOne(wrapper);
    }

    @Override
    public IPage<Reception> getMyReceptions(Page<Reception> page, String applicant) {
        LambdaQueryWrapper<Reception> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Reception::getApplicant, applicant)
              .orderByDesc(Reception::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public Reception submitReception(Reception reception) {
        // 1. 生成申请编号
        String applicationNo = generateApplicationNo();
        reception.setApplicationNo(applicationNo);
        
        // 2. 设置初始状态
        reception.setStatus("pending");
        
        // 3. 设置时间
        Date now = new Date();
        reception.setCreateTime(now);
        reception.setUpdateTime(now);
        
        // 4. 数据验证
        validateReception(reception);
        
        // 5. 保存数据
        this.save(reception);
        
        return reception;
    }
    
    private String generateApplicationNo() {
        // 生成申请编号：JD + 年月日 + 3位随机数
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        String random = String.format("%03d", new Random().nextInt(1000));
        return "JD" + dateStr + random;
    }
    
    private void validateReception(Reception reception) {
        // 1. 基本字段验证
        if (StringUtils.isEmpty(reception.getDepartment())) {
            throw new IllegalArgumentException("申请部门不能为空");
        }
        if (StringUtils.isEmpty(reception.getApplicant())) {
            throw new IllegalArgumentException("申请人不能为空");
        }
        if (StringUtils.isEmpty(reception.getGuestName())) {
            throw new IllegalArgumentException("接待对象不能为空");
        }
        if (StringUtils.isEmpty(reception.getGuestCompany())) {
            throw new IllegalArgumentException("来访单位不能为空");
        }
        if (reception.getGuestCount() == null || reception.getGuestCount() < 1) {
            throw new IllegalArgumentException("来访人数必须大于0");
        }
        
        // 2. 接待时间验证
        if (reception.getReceptionStartTime() == null) {
            throw new IllegalArgumentException("接待开始时间不能为空");
        }
        if (reception.getReceptionEndTime() == null) {
            throw new IllegalArgumentException("接待结束时间不能为空");
        }
        if (reception.getReceptionEndTime().before(reception.getReceptionStartTime())) {
            throw new IllegalArgumentException("接待结束时间不能早于开始时间");
        }
        
        // 3. 其他必填字段验证
        if (StringUtils.isEmpty(reception.getPurpose())) {
            throw new IllegalArgumentException("接待事由不能为空");
        }
        if (StringUtils.isEmpty(reception.getLocation())) {
            throw new IllegalArgumentException("接待地点不能为空");
        }
        if (StringUtils.isEmpty(reception.getReceptionType())) {
            throw new IllegalArgumentException("接待类型不能为空");
        }
        
        // 4. 费用相关验证
        if (reception.getEstimatedCost() == null || reception.getEstimatedCost() < 0) {
            throw new IllegalArgumentException("预计费用不能为负数");
        }
    }
} 